package com.example.test;

import android.content.Intent;
import android.net.VpnService;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.test.databinding.ActivityMainBinding;
import com.example.test.utils.NetWorkUtils;
import com.google.android.material.snackbar.Snackbar;

import engine.Engine;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "proxy";
    private ActivityMainBinding binding;
    private boolean isConnect = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.toolbar);
        isConnect = NetWorkUtils.isConnect(this);
        Log.d("网络是否可用",isConnect+"");

        Runnable startService = () -> {
            Snackbar.make(binding.fab, "Starting service", Snackbar.LENGTH_LONG).show();
            startService(new Intent(this, MyVpnService.class));
        };

        ActivityResultLauncher<Intent> getPermission = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() != RESULT_OK) {
                Snackbar.make(binding.fab, "Permission not granted!", Snackbar.LENGTH_LONG).show();
                return;
            }

            startService.run();
        });

        binding.fab.setOnClickListener(view -> {
            Intent intent = VpnService.prepare(this);
            if (intent != null) {
                getPermission.launch(intent);
                Snackbar.make(view, "Please grant permission", Snackbar.LENGTH_LONG).show();
            } else {
                startService.run();
            }
        });

        binding.tvIp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                Constants.IP = s.toString().trim();
                Log.d(TAG,"---代理ip为---"+Constants.IP);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.tvPort.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                Constants.PORT = s.toString().trim();
                Log.d(TAG,"---代理端口为---"+Constants.PORT);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.tvStop.setOnClickListener(view -> {
            try {
                //Intent intent = new Intent(this, MyVpnService.class);
               // startService(intent);
                Engine.stop();
                //stopService(intent);
            }catch (Exception e){
                Log.e(TAG,e.getMessage());
                e.printStackTrace();
            }
        });
    }
}